package com.tsinghuabigdata.edu.ddmath.MVPModel;

import com.tsinghuabigdata.edu.ddmath.bean.ClassInfo;
import com.tsinghuabigdata.edu.ddmath.bean.DoudouWork;
import com.tsinghuabigdata.edu.ddmath.bean.MyCourse;
import com.tsinghuabigdata.edu.ddmath.commons.http.AppAsyncTask;
import com.tsinghuabigdata.edu.ddmath.commons.http.HttpResponse;
import com.tsinghuabigdata.edu.ddmath.module.ddwork.bean.DDWorkDetail;
import com.tsinghuabigdata.edu.ddmath.requestHandler.MyLearnService;
import com.tsinghuabigdata.edu.ddmath.requestHandler.requestImpl.MyLearnServiceImpl;

import java.util.List;

/**
 * 我的学习
 */

public class MyStudyModel {

    private MyLearnService learnService = new MyLearnServiceImpl();



    public void  queryMyCourse(String accessToken, String studentId, String classId,
                               int pageNum, int pageSize, RequestListener requestListener) {
        QueryMyCourseTask queryMyCourseTask = new QueryMyCourseTask(requestListener);
        queryMyCourseTask.execute(accessToken, studentId, classId, pageNum + "", pageSize + "");
    }

    public void  queryClass(String accessToken, String classId, RequestListener requestListener) {
        QueryClassTask queryClassTask = new QueryClassTask(requestListener);
        queryClassTask.execute(accessToken, classId);
    }

    /**
     * 查询豆豆作业详情
     */
    public void  queryDDWorkDetail(String accessToken, String studentId, String workId, RequestListener requestListener) {
        QueryWorkDetailTask queryClassTask = new QueryWorkDetailTask(requestListener);
        queryClassTask.execute(accessToken, studentId, workId);
    }
	
	public void  queryDoudouWork(String accessToken, String studentId, int pageNum, int pageSize, int status, RequestListener requestListener) {
        QueryDoudouWorkTask queryMyCourseTask = new QueryDoudouWorkTask(requestListener);
        queryMyCourseTask.execute(accessToken, studentId, pageNum + "", pageSize + "", status + "");
    }

    /**
     * 查询班级的老师和所有学生信息
     */
    class QueryClassTask extends AppAsyncTask<String, Void, ClassInfo> {

        private RequestListener reqListener;

        public QueryClassTask(RequestListener requestListener) {
            reqListener = requestListener;
        }

        @Override
        protected ClassInfo doExecute(String... params) throws Exception {
            String accessToken = params[0];
            String classId = params[1];
            return learnService.queryClassInfo(accessToken, classId);
        }

        @Override
        protected void onResult(ClassInfo classInfo) {
            reqListener.onSuccess(classInfo);
        }

        @Override
        protected void onFailure(HttpResponse<ClassInfo> response, Exception ex) {
            reqListener.onFail(response, ex);
        }

    }

    /**
     * 查询我的课程
     */
    class QueryMyCourseTask extends AppAsyncTask<String, Void, List<MyCourse>> {

        private RequestListener reqListener;

        public QueryMyCourseTask(RequestListener requestListener) {
            reqListener = requestListener;
        }

        @Override
        protected List<MyCourse> doExecute(String... params) throws Exception {
            String accessToken = params[0];
            String studentId = params[1];
            String classId = params[2];
            int pageNum = Integer.parseInt(params[3]);
            int pageSize = Integer.parseInt(params[4]);
            return learnService.myCourse(accessToken, studentId, classId, pageNum, pageSize);
        }

        @Override
        protected void onResult(List<MyCourse> myCourses) {
            reqListener.onSuccess(myCourses);
        }

        @Override
        protected void onFailure(HttpResponse<List<MyCourse>> response, Exception ex) {
            reqListener.onFail(response, ex);
        }


    }

    /**
     * 学生作业列表查询
     */
    class QueryDoudouWorkTask extends AppAsyncTask<String, Void, DoudouWork> {

        private RequestListener reqListener;

        public QueryDoudouWorkTask(RequestListener requestListener) {
            reqListener = requestListener;
        }

        @Override
        protected DoudouWork doExecute(String... params) throws Exception {
            String accessToken = params[0];
            String studentId = params[1];
            int pageNum = Integer.parseInt(params[2]);
            int pageSize = Integer.parseInt(params[3]);
            int status = Integer.parseInt(params[4]);
            return learnService.queryDoudouWork(accessToken, studentId,pageNum, pageSize, status);
        }

        @Override
        protected void onResult(DoudouWork doudouWork) {
            reqListener.onSuccess(doudouWork);
        }

        @Override
        protected void onFailure(HttpResponse<DoudouWork> response, Exception ex) {
            reqListener.onFail(response, ex);
        }
    }
    
    class QueryWorkDetailTask extends AppAsyncTask<String, Void, DDWorkDetail> {

        private RequestListener reqListener;

        public QueryWorkDetailTask(RequestListener requestListener) {
            reqListener = requestListener;
        }

        @Override
        protected DDWorkDetail doExecute(String... params) throws Exception {
            String accessToken = params[0];
            String studentId = params[1];
            String workId = params[2];
            return learnService.getDDWorkDetail(accessToken, studentId, workId);
        }
        @Override
        protected void onResult(DDWorkDetail list) {
            reqListener.onSuccess(list);
        }
        @Override
        protected void onFailure(HttpResponse<DDWorkDetail> response, Exception ex) {
            reqListener.onFail(response, ex);
        }
    }

}
